#!groovy
pipeline {
    agent {
        label "$DITTO_BUILD_NODE"
    }
    parameters {
        string(name: 'EXTRA_MAVEN_ARGS', defaultValue: '', description: 'Additional maven arguments.')
        booleanParam(name: 'SKIP_SONAR_SCAN', defaultValue: false, description: 'Set to true to skip Sonarqube Scan.')
    }
    environment {
        // Need to replace the '%2F' used by Jenkins to deal with / in the path (e.g. story/...)
        theBranch = "${env.BRANCH_NAME}".replace("%2F", "-").replace("/", "-")
        theVersion = "0-${theBranch}-SNAPSHOT"
        theMvnRepo = "$WORKSPACE/../../.m2/feature-repository-${theBranch}"
        JAVA_TOOL_OPTIONS = '-Duser.home=/home/jenkins-slave'
    }
    options {
        buildDiscarder(logRotator(numToKeepStr: '10'))
        disableConcurrentBuilds()
        timeout(time: 1, unit: 'HOURS')
    }
    stages {
        stage('Clean workspace and create local maven repository') {
            steps {
                echo 'Clean workspace'
                cleanWs()
                echo "Create local maven repository"
                sh "mkdir -p ${theMvnRepo}"
            }
        }
        stage('Checkout scm') {
            steps {
                echo 'Checkout scm'
                checkout scm
            }
        }
        stage('Build') {
            agent {
                docker {
                    args "$DITTO_DOCKER_ARGUMENTS"
                    image "$DITTO_DOCKER_IMAGE_MAVEN_JDK_17"
                    reuseNode true
                }
            }
            steps {
                configFileProvider([configFile(fileId: 'mvn-bdc-settings', variable: 'MVN_SETTINGS')]) {
                    sh "mvn -s $MVN_SETTINGS clean deploy javadoc:jar source:jar " +
                               "-T1C --batch-mode --errors " +
                               "-Pditto " +
                               "-Drevision=${theVersion} " +
                               "${params.EXTRA_MAVEN_ARGS ?: ''}"
                }
            }
            post {
                always {
                    junit "**/**/target/surefire-reports/*.xml,**/**/**/target/surefire-reports/*.xml,**/**/**/target/failsafe-reports/*.xml,**/**/target/failsafe-reports/*.xml"
                }
            }
        }
        stage('SonarQube Scan') {
            agent {
                docker {
                    args "$DITTO_DOCKER_ARGUMENTS"
                    image "$DITTO_DOCKER_IMAGE_MAVEN_JDK_17"
                    reuseNode true
                }
            }
            when {
                expression {
                    return !params.SKIP_SONAR_SCAN
                }
            }
            steps {
                configFileProvider([configFile(fileId: 'mvn-bdc-settings', variable: 'MVN_SETTINGS')]) {
                    echo 'Sonar Scan'
                    withSonarQubeEnv(installationName: "${env.SONAR_QUBE_ENV}", credentialsId: 'sonarqube') {
                        sh "mvn -s $MVN_SETTINGS --batch-mode --errors $SONAR_MAVEN_GOAL " +
                                   "-Dsonar.host.url=$SONAR_HOST_URL " +
                                   "-Dsonar.projectKey=org.eclipse.ditto:${theBranch} " +
                                   "-Drevision=${theVersion}"
                    }
                }
            }
        }
    }
}

